<include file="Public:header" />
<div class="menu">
    <ul class="menubar">
        <li ><a href="index.php?m=api&a=index">个人主页</a></li>
        <li class="menu-value" ><a href="index.php?m=api&a=page">旅行日志</a></li>
        <li class="" ><a href="#">个人博客</a></li>
    </ul>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
	<style type="text/css">
	body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
	</style>
    <script src="http://cdn.bootcss.com/jquery/1.12.3/jquery.min.js"></script>
    <script src="__PUBLIC__/js/layer/layer.js"></script>
	<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=MiWcA3X21fcKg6vvq4Cjrj5GhPYrSGOj"></script>
	<title>地图展示</title>
</head>
<body>
	<div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
	// 百度地图API功能
	var map = new BMap.Map("allmap",{minZoom:4,maxZoom:6});    // 创建Map实例
	map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 初始化地图,设置中心点坐标和地图级别

    map.addControl(new BMap.NavigationControl({type: BMAP_NAVIGATION_CONTROL_SMALL}));
    map.enableScrollWheelZoom();
    function cityBound(city){
        var bdary = new BMap.Boundary();
        bdary.get(city, function(rs){       //获取行政区域
            console.log(rs)
                //这里是用户自己的函数。  
                var count = rs.boundaries.length; //行政区域的点有多少个
                        for(var i = 0; i < count; i++){
                            var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
                            map.addOverlay(ply);  //添加覆盖物
                        }      
        }); 

    }



    //map.setViewport(points);    //调整视野
	//添加地图类型控件
	map.addControl(new BMap.MapTypeControl({
		mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));	  
	map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的
	map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
	/*var label = new BMap.Label('这是一个标签', {
                        offset: new BMap.Size(20, -10)
                    });*/
/*var points = [
     [95.43511, 37.31229, '这是青海省'],
     [119.71698, 41.907, '这是辽宁省'], 
     [110.43058, 26.1442, '这是广西自治区'] 
   ];*/

    $.get('__APP__?m=api&a=mapList'+'&t=' + Math.random(1000), function(data){
        points = data.data;
        console.log(points)
        jiazai(points)

      }) 
   function jiazai(points){

    var pointArray = new Array();
	    for (var i = 0; i < points.length; i++) {
	        var marker = new BMap.Marker(new BMap.Point(points[i].lng, points[i].lat)); // 创建点
	        //addMarker(new BMap.Point(points[i][0], points[i][1]));
	        //console.log(points[i], points[i]);
	        map.addOverlay(marker); //增加点
	        var label = new BMap.Label(points[i].content, {
	            offset: new BMap.Size(20, -10)
	        });
	        marker.setLabel(label); //添加标签
	        pointArray[i] = new BMap.Point(points[i].lng, points[i].lat);
	        // 初始化不显示
	        label.setStyle({
	            display: "none"
	        });
	      // 鼠标经过时
	        marker.addEventListener("mouseover", function(e) {
	            var label = this.getLabel();
	            label.setStyle({
	                display: "block"
	            });
	        });
	        // 鼠标离开时
	        marker.addEventListener("mouseout", function(e) {
	            var label = this.getLabel();
	            label.setStyle({
	                display: "none"
	            });
	        });
	    }
    }
    	// 编写自定义函数,创建标注  并写入内容
	function addMarker(point,content){
	  var marker = new BMap.Marker(point);
	  map.addOverlay(marker);
      //写入内容
        var label = new BMap.Label(content, {
            offset: new BMap.Size(20, -10)
        });
        marker.setLabel(label); //添加标签
        // 初始化不显示
        label.setStyle({
            display: "none"
        });
      // 鼠标经过时
        marker.addEventListener("mouseover", function(e) {
            var label = this.getLabel();
            label.setStyle({
                display: "block"
            });
        });
        // 鼠标离开时
        marker.addEventListener("mouseout", function(e) {
            var label = this.getLabel();
            label.setStyle({
                display: "none"
            });
        });
	}
   //清除覆盖物
    function remove_overlay(){
        map.clearOverlays();         
    }

    //点击事件
    function showInfo(e){
        
        var geoc = new BMap.Geocoder();
        var pt = e.point;
        var city ='';
            geoc.getLocation(pt, function (rs) {
            var addComp = rs.addressComponents.province;
            cityBound(addComp)
            city = addComp;
        });

       
          layer.prompt({title: '写下计划：', formType: 2}, function(text, index){
            layer.close(index);
            
             $.ajax({
        		 type:"POST",
        		 url:"__APP__?m=api&a=mapAdd",
        		 dataType:"json",
        		 data:{text:text,lng:e.point.lng,lat: e.point.lat,city:city},	
        		 success: (function(data) {	
        		      	if (data.code==1000) {
        		      		layer.msg('标记成功');
        		      		//remove_overlay();
        		      		addMarker(new BMap.Point(e.point.lng, e.point.lat),text)
        		      	}else{
        		      		layer.msg('标记失败，这里你已经来过啦！');
        		      	}
        		   })       
        		 });
          });
    
		console.log(e.point.lng + ", " + e.point.lat);
	}
	map.addEventListener("click", showInfo);
</script>
